<?php
/**
* This page is used to delete a page.
* Deleting a page requires that the user be logged in and the owner of a personal wiki page, or a member of
*       a community.
*
* @package folio
* @param string $page The page title being deleted.  Required.
* @param string $user The user/community to whom this page belongs.
**/

    define("context", "folio_page_delete");
    // Run includes
    require_once("../../../includes.php");
    require_once("../model/page.php");
    require_once("../redirect.php");
    global $CFG;

    $title = __gettext("Delete Page");

    $_title = folio_page::title_decode(required_param('page'));
    $username = folio_clean( required_param('user') );
    $url = url;

    // Load Page information
    $page = folio_page::SelectWhereTitleAndUsername( $_title, $username );
    if( !$page ) {
        // Page hasn't been created yet.  Redirect to /new
        trigger_error('You can not delete a page that does not exist.', E_USER_ERROR);
    }

    // Test permissions.
    if( !$page->Permission('update') ) {
        // No permission.
        // User doesn't have permission to delete the page.
        $body = __gettext('You do not have permission to delete this page. ' .
            'You must be logged in, be deleting your own wiki page, ' .
            'or be a member of the community owning this page.');
    } else {
        // Permission.
        $body = __gettext('Click the below button to delete this page. Please note that deleting this page will also ' .
            'remove all child pages.  You will not be able to undo this ' .
            'action, so please be absolutely sure you want to delete this page.');

        $redirect = new folio_redirect;
        $redirect->redirect_url = "{$url}{$username}/page/";
        $redirect = $redirect->html();

        $body = <<< END
        <img src="{$CFG->wwwroot}mod/folio/image/page_delete.png"/><br>

    <form method="post" name="elggform" action="{$url}mod/folio/ajax/page_delete.php">
        {$body}
        {$redirect}
        <br/>
        <input type="hidden" name="page_ident" value="{$page->page_ident}" />
        <input type="submit" value="Delete" />
    </form>
    </div>

END;

    }

    header("Cache-control: private");
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $title,
                    'body' => $body
                ));
    echo templates_page_draw(array(
                    $title, $body
                ));
?>
